package cc.atliberty.creating.registerSingleton;

import java.util.HashMap;

/**
 * @Author 元宵
 * @url www.atliberty.cc
 * @Date 2023-10-30 18:18
 * @description: 登记式单例类
 */
public class RegSingleton {
    private static HashMap map = new HashMap();
    static {
        RegSingleton x = new RegSingleton();
        map.put(x.getClass().getName(), x);
    }
    
    // 用于被本包下的子类继承
    protected RegSingleton() {}
    
    public static RegSingleton getInstance(String name) {
        if (null == name) {
            name = RegSingleton.class.getName();
        }
        if (null==map.get(name)){
            try {
                // 创建对象
                map.put(name, Class.forName(name).newInstance());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } 
        }
        return ((RegSingleton) map.get(name));
    }
    
    public void about() {
        System.out.println("this is RegSingleton");
    }
}
